<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/15
 * Time: 10:59
 */
namespace app\index\Controller;

use think\Controller;
use think\Db;
use think\Request;
use think\Session;

class Order extends Common {
	/**
	 * 判断是否登录
	*/
	public function _initialize()
	{
		parent::_initialize();
	}
	/**
	 * 商家列表
	 * Method Get
	 * @param string get.title 要检索的字段
	 * @return Array result 商家信息
	*/
	public function index()
	{
		$db = Db::table('business')->order('id desc')->where('successively',1);
		$where = ['query'=>[]];
		if(!empty(input('get.title'))){
			$title = trim(input('get.title'));
			$db->where('name|linkman|telephone','like',"%{$title}%");
			$where['query']['title'] = $title;
		}
		$result = $db->paginate(8,false,$where);
		return view('index',compact('result','where'));
	}
	
	/**
	 * 商家商品列表
	 * Method Get
	 * @param string get.id 商家id
	 * @param string get.title 要检索的字段
	 * @return Array result 商家产品
	 * @return string id 商家id
	 * @return string title 检索的字段
	*/
	public function goods()
	{
		$bid = trim(input('get.id'));
		
		$db = Db::table('product')->where('bid',$bid)->order('id desc')
			->whereNotIn('status',[2,4,5]);
		$where = ['query'=>[]];
		$where['query']['id'] = $bid;
		if(!empty(input('get.title'))){
			$title = trim(input('get.title'));
			$db->where('pname|purchaseprice|retailprice|bid','like',"%{$title}%");
			$where['query']['title'] = $title;
		}
		$result = $db->paginate(8,false,$where);
		return view('goods',compact('result','title','bid'));
		
	}
	/**
	 * 删除商家商品
	 * Method Post
	 * @param string post.id 商家id
	 *
	 * @return json success 删除成功 error 删除失败错误信息
	*/
	public function delete()
	{
		if(Request()->isPost()){
			try{
				Db::startTrans();
				$result = Db::table('business')->where('id',input('post.id'))->update(['status'=>4]);
				if(!$result){
					throw new \PDOException("删除失败");
				}
				Db::commit();
				$success = ['success'=>'删除成功'];
				return json($success);
			}catch (\PDOException $e){
				Db::rollback();
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
	/**
	 * 删除商品
	 * Method Post
	 * @param string post.id  商品id
	 * @return json success 删除成功 error 删除失败错误信息
	*/
	public function goodsDel()
	{
		if(Request()->isPost()){
			Db::startTrans();
			try{
				$result = Db::table('product')->where('id',input('post.id'))->update(['status'=>2]);
				if(!$result){
					throw new \PDOException('删除失败');
				}
				Db::commit();
				$success = ['success'=>'删除成功'];
				return json($success);
			}catch (\PDOException $e){
				Db::rollback();
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
	/**
	 * 商品采购
	 * Method Get
	 * @param string get.id 商品id
	 * @param string get.bid 商家id
	*/
	public function purchase()
	{
		$id = explode(',',input('get.id'));
		$result = Db::table('product')->whereIn('id',$id)->select();
		$bid = input('get.bid');
		$successively = Db::table('business')->where('id',$bid)->value('successively');
		$shop = Db::table('shop')->where('status',1)->select();
		return view('purchase',compact('result','bid','shop','successively'));
	}
	
	/**
	 * 执行商品采购
	 * Method Post
	 * @param Array post.bid 商品id
	 * @param string post.address 地址
	 * @param Array post.number 采购数量
	 * @return json success 采购成功 error 采购失败错误信息
	*/
	public function doPurchase()
	{
		if(Request()->isAjax()){
			try{
				Db::startTrans();
				$pid = explode(',',input('post.pid'));
				$input['bid'] = input('post.bid');
				$city = input('post.city');
				$payment = input('post.payment');
				if(!$payment){
					throw new \PDOException('请选择付款方式');
				}
				$number = explode(',',input('post.num'));
				$input['sid'] = Db::table('shop')->where('address',$city)->value('id');
				if(!$input['sid']){
					throw new \PDOException('请选择店铺');
				}
				$input['ordernumber'] = time();
				$input['date'] = date('Y-m-d H:i:s',time());
				$input['describe'] = input('post.describe');
				for($i=0;$i<count($pid);$i++){
					$purchaseprice = Db::table('product')->where('id',$pid[$i])->value('purchaseprice');
					$input['total'] = $purchaseprice * $number[$i];
					$input['pid'] = $pid[$i];
					$input['number'] = $number[$i];
					if($payment == '1' ){
						$result[] = Db::table('purchase')->insertGetId($input);
					}
					if($payment == '2'){
						$result[] = Db::table('postpayment')->insertGetId($input);
					}
					
				}
				if(!is_array($result)){
					throw new \PDOException('采购失败');
				}
				Db::commit();
				$success = ['success' => '采购成功'];
				return json($success);
			}catch (\PDOException $e){
				Db::rollback();
				$error = ['error' => $e->getMessage()];
				return json($error);
			}
			
		}
	}
	/**
	 * 商家订单
	 * @param string get.bid 商家id
	 * @param string get.title 要检索的字段
	 * @return Array result 查询结果
	*/
	public function order()
	{
		$where = ['query'=>[]];
		$bid = input('get.bid');
		$db = Db::table('purchase')
			->field('product.pname,product.purchaseprice,product.retailprice,purchase.number,purchase.total,
					purchase.actualtotal,purchase.ordernumber,purchase.date,business.linkman,business.telephone,
					purchase.id,purchase.pmethod')
			->join('business','business.id = purchase.bid','RIGHT')
			->join('shop','shop.id = purchase.sid','LEFT')
			->join('product','product.id = purchase.pid','LEFT')
			->where('business.id',$bid)
			->where('purchase.status','<>',0)
			->order('purchase.id desc')
			->group('ordernumber');
		if(!empty(trim(input('get.title')))){
			$title = trim(input('get.title'));
			$db->where('product.pname|purchase.ordernumber','like',"%{$title}%");
			$where['query']['title'] = $title;
		}
		$where['query']['bid'] = $bid;
		$result = $db->paginate(8,false,$where);
		return view('order',compact('result','title','bid'));
	}
	/**
	 * 订单详情
	*/
	public function detail()
	{
		$result = Db::table('purchase')
				->field('purchase.id,purchase.number,purchase.total,purchase.actualtotal,
						product.pname,product.purchaseprice,product.retailprice,purchase.ordernumber,purchase.date')
				->join('business','business.id = purchase.id','LEFT')
				->join('shop','shop.id = purchase.sid','LEFT')
				->join('product','product.id = purchase.pid','LEFT')
				->where('purchase.ordernumber',input('get.ordernumber'))
				->where('purchase.status','<>',0)
				->paginate(8);
		
		return view('details',compact('result'));
	}
	/**
	 * 订单详情
	 * @param string get.id 订单id
	 * @return Array result 查询结果
	*/
	public function orderDetail()
	{
		$result = Db::table('purchase')
				->join('business','business.id = purchase.bid','LEFT')
				->join('shop','shop.id = purchase.sid','LEFT')
				->join('product','product.id = purchase.pid','LEFT')
				->where('purchase.id',input('get.id'))
				->find();
		$result['image'] = json_decode($result['image']);
		return view('detail',compact('result'));
	}
	
	/**
	 * 删除商家订单
	 * @param string post.id 订单id
	 * @return json success 删除成功 error 删除失败
	*/
	public function orderDel()
	{
		if(request()->isAjax()){
			try{
				$id = input('post.id');
				$result = Db::table('purchase')->where('id',$id)->update(['status'=>0]);
				if(!$result){
					throw new \PDOException('删除失败');
				}
				$success = ['success'=>'删除成功'];
				return json($success);
			}catch (\PDOException $e){
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
	
}
